/*
 layui.use(['layer','form'], function(){
 var $ = layui.jquery;
 var form = layui.form();
 $("#addBtn").bind("click",function(){
 var url = $(this).data('url');
 var title = $(this).data('title');
 var width = $(this).data('width');
 var height = $(this).data('height');
 console.log(url);
 var layer = layui.layer;
 $.get(url, {}, function(str) {
 layer.open({
 maxmin: true,
 type: 1,
 title: title,
 area: [width, height],
 btn: ['保存', '取消'],
 yes: function (index, layero) {
 //触发表单的提交事件
 console.log("到这里了！！！")
 $("#submitBtn").click();
 },
 content: str, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
 success: function (layero, index) {
 form.render();
 //监听提交
 form.on('submit(submitBtn)', function (data) {
 var d = JSON.stringify(data.field);
 console.log(data);
 var action = data.form.action;
 $.post(action, data.field, function (dd) {
 console.log(dd);
 if (dd.success) {
 layer.open({title: '成功', content: '保存成功'});
 location.reload(true);
 } else {
 layer.open({title: '失败', content: '保存失败'});

 }, "json");

 return false;
 });
 }

 });
 });
 });
 });*/

layui.use(['layer','form'], function() {
    var $ = layui.jquery;
    var addBoxIndex = -1;
    //添加按钮事件
    $(".addBtn").on("click", function () {
        console.log("addBoxIndex : " + addBoxIndex);
        if (addBoxIndex !== -1) {
            return;
        }
        var url = $(this).data('url');
        var title = $(this).data('title');
        var width = $(this).data('width');
        var height = $(this).data('height');
        var layer = layui.layer;
        console.log(url);
        addBoxIndex = layer.open({
            maxmin: true,
            type: 2,
            title: title,
            area: [width, height],
            btn: ['保存', '取消'],
            yes: function (index, layero) {
                //触发表单的提交事件
                var body = layer.getChildFrame('form', index);
                //body.find("#submitBtn").click();
                body.find('button[lay-filter=submitBtn]').click();
            },
            content: url, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            success: function (layero, index) {

            },
            end: function () {
                addBoxIndex = -1;
            }
        });
        return false;
    });
    var editBoxIndex = -1;
    $("body").on("click", ".editBtn", function () {
        console.log("editBoxIndex : " + editBoxIndex);
        if (editBoxIndex !== -1) {
            return;
        }
        var url = $(this).data('url');
        var title = $(this).data('title');
        var width = $(this).data('width');
        var height = $(this).data('height');
        var layer = layui.layer;
        editBoxIndex = layer.open({
            maxmin: true,
            type: 2,
            title: title,
            area: [width, height],
            btn: ['保存', '取消'],
            yes: function (index, layero) {
                //触发表单的提交事件
                var body = layer.getChildFrame('form', index);
                //body.find("#submitBtn").click();
                body.find('button[lay-filter=submitBtn]').click();
            },
            content: url, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            success: function (layero, index) {

            },
            end: function () {
                editBoxIndex = -1;
            }
        });
    });
    var detailBoxIndex = -1;
    $("body").on("click", ".detailBtn", function () {
        console.log("detailBoxIndex : " + detailBoxIndex);
        if (detailBoxIndex !== -1) {
            return;
        }
        var url = $(this).data('url');
        var title = $(this).data('title');
        var width = $(this).data('width');
        var height = $(this).data('height');
        var layer = layui.layer;
        detailBoxIndex = layer.open({
            maxmin: true,
            type: 2,
            title: title,
            area: [width, height],
            content: url, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            success: function (layero, index) {

            },
            end: function () {
                detailBoxIndex = -1;
            }
        });
    });
    $("body").on("click", ".deleteBtn", function () {
        var url = $(this).data('url');
        var title = $(this).data('title');
        var width = $(this).data('width');
        var height = $(this).data('height');
        var layer = layui.layer;
        var form = layui.form();
        layer.confirm("确认删除吗？",
            {icon: 2, title: "删除"},
            function (index) {
                $.post(url, {}, function (dd) {
                    if (dd.success) {
                        layer.open({
                            title: '删除成功', btn: "确定", type: '1', btnAlign: 'c',
                            yes: function (i) {
                                console.log(i);
                                //var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                //parent.layer.close(index); //再执行关闭
                                layer.close(i);
                                location.reload(true);
                            },
                            cancel: function (i) {
                                //var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                //parent.layer.close(index); //再执行关闭
                                layer.close(i);
                                location.reload(true);

                            }
                        });
                    } else {
                        layer.open({title: '删除失败', type: '1', btnAlign: 'c'});
                    }
                }, "json");
            }
        );
     });


    var form = layui.form();
    //form.render();
    //监听提交
    form.on('submit(submitBtn)', function (data) {
        var d = JSON.stringify(data.field);
        var action = data.form.action;
        console.log(action);
        $.post(action, data.field, function (dd) {
            if (dd.success) {
                parent.layer.open({
                    title: '保存成功', btn: "确定",type:'1',btnAlign: 'c',
                    yes: function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.location.reload(true);
                    },
                    cancel: function (){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.location.reload(true);
                    }
                });
            } else {
                layer.open({title: '保存失败',type:'1',btnAlign: 'c'});
            }
        }, "json");
        return false;
    });
    /*监听页码*/
    form.on('select(pageChange)', function(data){
        console.log(data.value); //得到被选中的值
        var page = location.search.match(/pageNo=(\d+)/);
        page = page ? page[1] : 1;
        var pageSize = data.value;
        location.href = '?pageSize=' + pageSize +'&pageNo='+page;
    });

    form.verify({
        username: function(value){
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                return '用户名不能有特殊字符';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '用户名首尾不能出现下划线\'_\'';
            }
            if(/^\d+\d+\d$/.test(value)){
                return '用户名不能全为数字';
            }
        }

        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        ,pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
    });
});